import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from po.navigation_bar import MyAssets
from po.base_page import BasePage


class BuyFund(BasePage):

    def buy_fund(self):
        """
        认购/申购
        :return:
        """

        title = self.driver.find_element(By.CLASS_NAME, 'd2-theme-item-ld-aside')
        ActionChains(self.driver).move_to_element(title).perform()
        time.sleep(2)  # 子账户过多时，页面需要全部加载完毕
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div[2]/div[1]/div/ul/li[1]/div[2]/div/div[1]/span/span').click()         # 点击公募产品
        self.driver.find_element(By.XPATH, '//*[@id="pane-HB"]/div/span/div[2]/div[5]/div[2]/table/tbody/tr[1]/td[14]/div/div[1]').click()   # 点击购买

        input_element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div[1]/form/div[1]/div[2]/div/div/input')))
        input_element.click()   # 点击账户

        account = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child(1)')))
        action = ActionChains(self.driver)
        action.move_to_element(account).perform()
        action.click(account).perform()  # 选择账户

        self.driver.find_element(By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div[1]/form/div[2]/div[2]/div/div[1]/div[6]/input').send_keys(5)   # 输入金额
        self.driver.find_element(By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div[1]/form/div[5]/div/button').click()    # 点击确认
        # send_pass = WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located(
        #         (By.XPATH, '/html/body/div[1]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div/div[1]/div/div[2]/div[2]/div/input')))
        # send_pass.send_keys('q111111')  #输入密码
        time.sleep(2)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[1]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div/div[1]/div/div[2]/div[2]/div/input').send_keys('q111111')
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div/div[1]/div/div[2]/div[3]/button[2]').click()

    def withdraw_order(self):
        """
        撤单
        :return:
        """
        title = MyAssets(self.driver)
        title.choose_tab_by_content_item('交易记录')
        self.driver.find_element(By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div[2]/div[2]/div[2]/div[1]/div[4]/div[2]/table/tbody/tr[1]/td[9]/div/div/span[2]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/form/div[1]/div/div/input').send_keys('q111111')
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/form/div[3]/div/button').click()

    def convert(self):
        """
        转换
        :return:
        """
        title = MyAssets(self.driver)
        title.choose_tab_by_content_item('我的资产')
        self.driver.find_element(By.XPATH, '//*[@id="pane-now"]/div[1]/div[5]/div[2]/table/tbody/tr[1]/td[8]/div/p/a[2]/img').click()
        time.sleep(1)
        convert_element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div/form/div[2]/div[2]/div/div[2]/span/span')))
        convert_element.click()     # 选择转换的基金

        convert_fund = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.CSS_SELECTOR,
                 '#APPBODY > div.container-component > div > div > div > div.el-drawer__wrapper > div > div > section > div.header > div.el-select.fund-code.el-select--default > div.el-input.el-input--default.el-input--prefix.el-input--suffix > input')))
        convert_fund.send_keys('000326')

        select_fund = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            'body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li > div')))
        action = ActionChains(self.driver)
        action.move_to_element(select_fund).perform()
        action.click(select_fund).perform()  # 选择基金

        convert_element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH,
                 '//*[@id="APPBODY"]/div[1]/div/div/div/div[1]/div/div/section/div[2]/div[1]/div/div[5]/div[2]/table/tbody/tr/td[8]/div/span/button/span')))
        ac = ActionChains(self.driver)
        ac.move_to_element(convert_element).perform()
        ac.click(convert_element).perform()  # 点击转换

        time.sleep(2)
        send_money = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH,
                 '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div/form/div[6]/div[2]/div/input')))
        send_money.send_keys('200')    # 输入金额

        self.driver.find_element(By.XPATH, '//*[@id="APPBODY"]/div[1]/div/div/div/section[2]/div/form/div[8]/div/button').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[1]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div/input').send_keys('q111111')
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[1]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div/div[2]/div[3]/button[2]').click()